Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: clean up generic impls #52827

Merged
merged 7 commits into from
Aug 4, 2018
Merged

Conversation

GuillaumeGomez
Copy link
Member

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 29, 2018
@QuietMisdreavus QuietMisdreavus changed the title Generic impls rustdoc: clean up generic impls Jul 29, 2018
@rust-highfive
Copy link
Contributor

The job x86_64-gnu-llvm-5.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:32:11]    Compiling aho-corasick v0.6.6
[00:32:15]    Compiling tempfile v3.0.2
[00:32:33]    Compiling minifier v0.0.14
[00:32:35]    Compiling rustdoc v0.0.0 (file:///checkout/src/librustdoc)
[00:32:36] error: trait objects without an explicit `dyn` are deprecated
[00:32:36]   --> librustdoc/clean/def_ctor.rs:19:14
[00:32:36]    |
[00:32:36] 19 | where F: Fn(&Fn(DefId) -> Def) -> Vec<Item> {
[00:32:36]    |              ^^^^^^^^^^^^^^^^ help: use `dyn`: `dyn Fn(DefId) -> Def`
[00:32:36]    |
[00:32:36]    = note: requested on the command line with `-D bare-trait-objects`
[00:32:36] 
[00:32:36] error: trait objects without an explicit `dyn` are deprecated
[00:32:36]   --> librustdoc/clean/def_ctor.rs:56:14
[00:32:36]    |
[00:32:36] 56 | where F: Fn(&Fn(DefId) -> Def, String) -> Vec<Item> {
[00:32:36]    |              ^^^^^^^^^^^^^^^^ help: use `dyn`: `dyn Fn(DefId) -> Def`
travis_time:end:19d124f0:start=1532876703082642932,finish=1532878662741449220,duration=1959658806288

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:0acacda8

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@GuillaumeGomez
Copy link
Member Author

cc @eddyb

use self::finder_trait::Finder;

pub struct BlanketImplFinder<'a, 'tcx: 'a, 'rcx: 'a> {
pub cx: &'a core::DocContext<'a, 'tcx, 'rcx>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just add methods to DocContext? Also, I'd rename the methods so they all start with get_blanket_impls_for_... instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Moving into DocContext.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's weird to move it directly into DocContext... Why would the DocContext provide for blanket impls?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's the global object of rustdoc and this is part of what rustdoc needs to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever this PR is merged, i'd like to move the auto/blanket impl collection (as well as all the other explicit impl collection) into an early pass, so the code can be isolated over there instead.

def_id: DefId,
callback: &F,
) -> Vec<Item>
where F: Fn(& dyn Fn(DefId) -> Def) -> Vec<Item> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this generally by replacing Vec<Item> with R. Also you can get the DefId from TyAdt , so why not just return an Option<Def>, with no callback?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filtering is done before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why would it be better to replace Vec<Item> with R?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Vec<Item> is very arbitrary and the function doesn't have anything to do with Vec<Item>, that's just what the closures it's used with deal with.

Returning Option<Def> is probably a better idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is still outstanding, but has been hidden by future commits editing the area around it.

I would prefer to see a generic return value - that's how these callback methods tend to work. However, since there is that arm returning Vec::new(), we should either depend on the return value implementing Default, or restructure it so that a callback is not necessary.


use super::*;

pub fn get_def_ctor_from_def_id<F>(cx: &DocContext,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a name like get_def_from_ty. I'm guessing this is mostly for aliases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly yes.

// Instead, we construct 'fake' def ids, which start immediately after the last DefId in
// DefIndexAddressSpace::Low. In the Debug impl for clean::Item, we explicitly check for fake
// def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
fn next_def_id(&self, crate_num: CrateNum) -> DefId {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you need new DefIds?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid them getting filtered out because of duplicates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah hold on, it's required for the auto traits (sync and send) but not be for blanket impls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's required for blanket impls as well otherwise they're counted as duplicates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should change the deduplication logic instead, then!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep but nothing to do with this PR. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every clean::Item needs a DefId. As the synthetic impls for auto traits (and hopefully eventually, blanket traits too) invent an impl out of thin air, one needs to be created so that the Item can hold it.

The real question is whether we can get away with only creating clean::Impl structs, and ignoring/inferring the fields on Item, like its attributes, visibility, stability, or deprecation. These can probably be inferred from the trait itself (or the blanket impl being applied), so that would help to prevent us from requiring new DefIds.

Copy link
Member

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for refactoring this! I have a couple comments.

use super::*;

pub trait Finder<'a, 'tcx: 'a, 'rcx: 'a> {
fn get_cx(&self) -> &DocContext<'a, 'tcx, 'rcx>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why these methods need to exist on a trait like this, if all the implementor needs to do is supply a DocContext. Could we instead add them to DocContext?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it's just to be able to implement some methods directly inside the trait (and therefore avoid code duplication).

I like to keep things grouped by functionalities. Also, AutoTraitFinder requires an additional field to work so I think merging it inside DocContext would be a mistake. Therefore, I think keeping the BlanketImplFinder struct on its own would be better. However, if you prefer me to merge it inside DocContext, I'll.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To pull the conversation out of discord...)

I'm not talking about AutoTraitFinder or BlanketImplFinder. Those are distinct enough to be on their own. I'm thinking more about just the provided methods on the Finder trait itself.

// we need to reexport something from libstd so that `all_trait_implementations` is called.
pub use std::string::String;

include!("primitive/primitive-generic-impl.rs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use include!() here? Did it affect the result of this test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replicated how it was performed in libstd. Since it's the main target of this fix, I thought it was the best way.

// Instead, we construct 'fake' def ids, which start immediately after the last DefId in
// DefIndexAddressSpace::Low. In the Debug impl for clean::Item, we explicitly check for fake
// def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
fn next_def_id(&self, crate_num: CrateNum) -> DefId {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every clean::Item needs a DefId. As the synthetic impls for auto traits (and hopefully eventually, blanket traits too) invent an impl out of thin air, one needs to be created so that the Item can hold it.

The real question is whether we can get away with only creating clean::Impl structs, and ignoring/inferring the fields on Item, like its attributes, visibility, stability, or deprecation. These can probably be inferred from the trait itself (or the blanket impl being applied), so that would help to prevent us from requiring new DefIds.

@QuietMisdreavus
Copy link
Member

Outstanding comments:

  • The callback in get_def_from_(def|node)_id can be expressed generically, with the closure and the function returning some R: Default, or otherwise refactoring the function so that the closure is not necessary. I'm okay leaving this as-is, since it can be modified without moving as much code as the rest of this PR already does.
  • We should investigate whether we need to store full clean::Items for synthetic impls. If we can get away without storing a full Item, we don't have to generate fake DefIds either, which cleans up a major point of contention from @eddyb. While this would be a great thing to do, this PR is already big enough (and apparently has already had to undergo a rebase for a merge conflict). It can be handled separately.

Otherwise, this looks ready to go! If we can get this landed, we can build farther refactors on top of it.

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 4, 2018

📌 Commit d57f3a6 has been approved by QuietMisdreavus

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 4, 2018
@bors
Copy link
Collaborator

bors commented Aug 4, 2018

⌛ Testing commit d57f3a6 with merge 667fdc1...

bors added a commit that referenced this pull request Aug 4, 2018
@bors
Copy link
Collaborator

bors commented Aug 4, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: QuietMisdreavus
Pushing 667fdc1 to master...

@bors bors merged commit d57f3a6 into rust-lang:master Aug 4, 2018
@GuillaumeGomez GuillaumeGomez deleted the generic-impls branch August 5, 2018 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants